import 'package:dk_flutter_base/dk_ui.dart';

class TransSelectBottom extends StatelessWidget {
  final List<SelectModel> list;
  final TransSelectBottomOption option;

  TransSelectBottom(this.list, this.option);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      margin: EdgeInsets.fromLTRB(7.5, 0, 7.5, 20),
      child: Column(
        children: <Widget>[
          Material(
            color: Colors.white,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(15.0),
            ),
            child: ListView.builder(
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int index) {
                  return index == list.length - 1
                      ? getButton(index, context)
                      : getBottomLineItem(index, context);
                },
                itemCount: list.length),
          ),
          Divider(
            height: 10,
            color: Colors.transparent,
          ),
          MaterialButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            minWidth: double.infinity,
            color: Colors.white,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(15.0),
            ),
            height: option.itemHeight,
            child: Text(
              option.cancelContent,
              style: TextStyle(
                  fontSize: option.fontSize, color: option.cancelColor),
            ),
          ),
        ],
      ),
    );
  }

  Widget getBottomLineItem(int index, BuildContext context) {
    return Column(
      children: <Widget>[
        getButton(index, context),
        Divider(height: 0.5, indent: 9, color: Color(0xffDBDBDB)),
      ],
    );
  }

  Widget getButton(int index, BuildContext context) {
    return MaterialButton(
      onPressed: () {
        Navigator.of(context).pop(list[index]);
      },
      minWidth: double.infinity,
      height: option.itemHeight,
      child: Text(
        list[index].value,
        style: TextStyle(fontSize: option.fontSize, color: option.itemColor),
      ),
    );
  }
}

class TransSelectBottomOption {
  double itemHeight;
  double fontSize;
  Color itemColor;
  Color cancelColor;
  String cancelContent;

  TransSelectBottomOption(
      {this.itemHeight,
      this.fontSize,
      this.itemColor,
      this.cancelColor,
      this.cancelContent}) {
    itemHeight ??= 57.0;
    fontSize ??= 18.0;
    itemColor ??= Color(0xFF3DBCC6);
    cancelColor ??= Color(0xff999999);
    cancelContent ??= "取消";
  }

  double getTotalHeight(int length) {
    return (length + 1) * itemHeight + 30 + (length - 1) * 0.5;
  }
}
